home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AmigActive 10
/
AACD 10.iso
/
AACD
/
Magazine
/
Online
/
httpproxy
/
src
/
queue.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-08-20
|
5KB
|
263 lines
/*(( "Header" */
/*
* $Id: queue.c,v 1.3 1996/08/20 17:36:43 mshopf Exp mshopf $
*
* (c) 1995-96 Matthias Hopf
*
* Queueing for HttpProxy.
*
*/
/*
* $Log: queue.c,v $
* Revision 1.3 1996/08/20 17:36:43 mshopf
* writing url entries on queuing and dequeuing, too.
* changed url file format (alpha compatible).
* small bug fix.
*
* Revision 1.2 1996/08/11 22:25:15 mshopf
* reworked debug messages.
*
* Revision 1.1 1996/07/30 13:57:03 mshopf
* Initial revision
*
*
*/
/*)) */
/*(( "Queuefile format" */
/*
* Format of the queue file in EBNF:
*
* Queuefile ::= { QueueEntry }*
* QueueEntry ::= Type Url '\n'
* Type ::= AddUrl | RemoveUrl
* AddUrl ::= '+' | '' ('' only for compatibility - will be removed next time)
* RemoveUrl ::= '-'
*
* Partial semi-format description of all low-level symbols:
*
* Url ::= { printable }*
*/
/*)) */
/*(("Includes/Constants" */
#include <string.h>
#include <exec/exec.h>
#include <dos.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include "queue.h"
#include "logging.h"
/*)) */
/*(( "Globals" */
/* global queue table */
static queue_t Queue [MAX_QUEUE_NUM]; /* initialized with '\0' */
static int Initialized = FALSE;
/*)) */
/*(( "QueueWriteEntry ()" */
/* open the queue file and write a queue entry */
/* doesn't do that while we are building the entry table (Initialized==FALSE). */
static void QueueWriteEntry (char Type, const char *Url)
{
BPTR File;
if (Initialized)
{
if (! (File = Open (QUEUEFILE, MODE_READWRITE)) )
{
LogErr (NULL, L_WARN, NULL, -1, "cannot open queuefile");
return;
}
SetIoErr (0);
Seek (File, 0, OFFSET_END);
FPutC (File, (LONG) Type);
FPuts (File, (char *) Url);
FPutC (File, '\n');
if (IoErr ())
LogErr (NULL, L_ERROR, NULL, -1, "error while writing to queuefile");
Close (File);
}
}
/*)) */
/*(( "QueueInit ()" */
/* Initialization. Read in queuing information */
int QueueInit (void)
{
int i = 0;
BPTR File;
char *c;
if (Initialized)
return 1;
if (! (File = Open (QUEUEFILE, MODE_OLDFILE)) )
{
LogErr (NULL, L_WARN, NULL, -1, "cannot open queuefile");
Initialized = TRUE;
return 0;
}
do
if (FGets (File, Queue [i] .Url, MAX_QUEUE_LEN))
{
if ( (c = strrchr (Queue [i] .Url, '\n')) )
*c = 0;
switch (Queue [i] .Url [0]) {
case '+':
/* move Url one byte to the left (including '\0') */
memmove (& Queue [i] .Url [0], & Queue [i] .Url [1], strlen (Queue [i] .Url));
break;
case '-':
/* remove Url from list. The current entry won't be found because it starts with '-' */
QueueUnqueue (QueueCheck (& Queue [i] .Url [1]));
Queue [i--] .Url [0] = '\0'; /* to be filled again */
break;
default:
/* After compatibility remove: error message */
break;
}
debug (D_QUEUE, ("init: '%s'\n", Queue [i] .Url));
}
else
break;
while (++i < MAX_QUEUE_NUM);
i = 1;
if (IoErr ())
{
LogErr (NULL, L_ERROR, NULL, -1, "error while reading queuefile");
i = 0;
}
Close (File);
Initialized = TRUE;
return i;
}
/*)) */
/*(( "QueueExit ()" */
/* Clean up. Save queuing information */
void QueueExit (void)
{
int i;
BPTR File;
if (! Initialized)
return;
Initialized = FALSE;
if (! (File = Open (QUEUEFILE "@new", MODE_NEWFILE)) )
{
LogErr (NULL, L_WARN, NULL, -1, "cannot open new queuefile");
return;
}
SetIoErr (0);
for (i = 0; i < MAX_QUEUE_NUM; i++)
if (Queue [i] .Url [0] != '\0')
{
FPutC (File, '+');
FPuts (File, Queue [i] .Url);
FPutC (File, '\n');
debug (D_QUEUE, ("exit: '%s'\n", Queue [i] .Url));
Queue [i] .Url [0] = '\0';
}
if (IoErr ())
LogErr (NULL, L_ERROR, NULL, -1, "error while writing new queuefile");
Close (File);
DeleteFile (QUEUEFILE);
Rename (QUEUEFILE "@new", QUEUEFILE);
}
/*)) */
/*(( "QueueCheck ()" */
/* Check whether a specific Url is already queued. */
queue_t *QueueCheck (const char *Url)
{
int i;
debug (D_QUEUE, ("check: Url '%s'\n", Url));
for (i = 0; i < MAX_QUEUE_NUM; i++)
if (strcmp (Url, Queue [i] .Url) == 0)
return (& Queue [i]);
return (NULL);
}
/*)) */
/*(( "QueueQueue ()" */
/* Queue a Url. */
int QueueQueue (const char *Url)
{
int i;
debug (D_QUEUE, ("queue: Url '%s'\n", Url));
if (strlen (Url) >= MAX_QUEUE_LEN)
dreturn (D_QUEUE, 0);
if (QueueCheck (Url))
dreturn (D_QUEUE, 1);
for (i = 0; i < MAX_QUEUE_NUM; i++)
if (Queue [i] .Url [0] == '\0')
{
strcpy (Queue [i] .Url, Url);
QueueWriteEntry ('+', Url);
dreturn (D_QUEUE, 1);
}
dreturn (D_QUEUE, 0);
}
/*)) */
/*(( "QueueUnqueue ()" */
/* Remove a queued Url. */
void QueueUnqueue (queue_t *q)
{
if (! q)
return;
debug (D_QUEUE, ("unqueue: Url '%s'\n", q->Url));
QueueWriteEntry ('-', q->Url);
q->Url [0] = '\0';
}
/*)) */
/*(( "QueueNext ()" */
/* Enumeration function. Return next queued Url. */
queue_t *QueueNext (const queue_t *Last)
{
const queue_t *q;
for (q = Last ? Last + 1 : Queue; q < & Queue [MAX_QUEUE_NUM]; q++)
if (q->Url [0] != '\0')
return q;
return NULL;
}
/*)) */